import 'package:flutter/material.dart';
import '../input/suggestion_overlay.dart';
import '../input/input_edit_widget.dart';
import 'gkb_dialog.dart';

class GKBInputDialogConfig {
  final String? title;
  final TextStyle? titleTextStyle;
  final String? leftText;
  final TextStyle? inputTextStyle;
  final String? rightText;
  final String? hintText;
  final TextInputType? keyboardType;
  final String? content;
  final double? height;
  final double? paddingHeight;
  final bool? autofocus;
  final Widget? child;
  final TextEditingController? controller;
  final bool hiddenCancel;
  final List<String> suggestion;

  GKBInputDialogConfig(
      {this.title,
      this.titleTextStyle,
      this.leftText,
      this.rightText,
      this.inputTextStyle,
      this.hintText,
      this.keyboardType,
      this.content,
      this.height,
      this.paddingHeight,
      this.autofocus,
      this.child,
      this.controller,
      this.suggestion = const [],
      this.hiddenCancel = false});
}

class GKBInputDialog {
  static void show({
    required BuildContext context,
    final Function(String value)? onTextChange,
    required GKBInputDialogConfig config,
    final VoidCallback? onCancel,
    final VoidCallback? onConfirm,
  }) {
    final FocusNode _focusNode = FocusNode();
    showDialog(
        context: context,
        barrierDismissible: true,
        routeSettings: RouteSettings(
            name: 'GKBInputDialog',
            arguments: GKBDialogArguments(
                title: config.title, content: config.content)),
        builder: (context) {
          TextEditingController _textController =
              config.controller ?? TextEditingController();
          Widget textField = config.child ??
              InputEditWidget(
                  focusNode: _focusNode,
                  hintText: config.hintText ?? '',
                  textStyle: config.inputTextStyle,
                  isOutLine: true,
                  autofocus: config.autofocus ?? true,
                  controller: _textController,
                  contentPadding: const EdgeInsets.only(left: 10, right: 10),
                  keyboardType: config.keyboardType,
                  textChange: onTextChange);
          return Scaffold(
            backgroundColor: Colors.transparent,
            body: BaseDialog(
                title: config.title,
                titleTextStyle: config.titleTextStyle ??
                    const TextStyle(
                      fontSize: 12,
                      color: Color(0xff222222),
                    ),
                leftText: config.leftText,
                rightText: config.rightText,
                onCancel: onCancel,
                onConfirm: onConfirm,
                content: Container(
                  //height: height ?? 40,
                  constraints: BoxConstraints(minHeight: config.height ?? 40),
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Visibility(
                          visible: config.content != null,
                          child: Padding(
                            padding: const EdgeInsets.only(bottom: 10),
                            child: Text(
                              config.content ?? '',
                              style: const TextStyle(fontSize: 12.0),
                              textAlign: TextAlign.center,
                            ),
                          )),
                      SizedBox(
                        height: 40,
                        child: config.suggestion.isEmpty
                            ? textField
                            : SuggestionOverlay(
                                focusNode: _focusNode,
                                textField: textField,
                                suggestion: config.suggestion,
                                controller: _textController),
                      ),
                    ],
                  ),
                ),
                hiddenCancel: config.hiddenCancel),
          );
        });
  }
}
